package com.nanychuan.blogback.service.impl;

import com.nanychuan.blogback.mapper.ArticleMapper;
import com.nanychuan.blogback.mapper.CategoryMapper;
import com.nanychuan.blogback.pojo.Article;
import com.nanychuan.blogback.pojo.Category;
import com.nanychuan.blogback.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author 往
 * 博客文章前台展示
 */
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Article> selectAll(String uid,String title) {

/*
        //查询全部文章
        List<Article> articles = articleMapper.selectAll();
        //返回文章集合
        return articles;
*/

        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        //查询当前登录用户的文章
        criteria.andEqualTo("uid",uid);
        if(title!=null && !title.equals("")){
            criteria.andLike("title","%" + title + "%");
        }
        List<Article> articles = articleMapper.selectByExample(example);
        //遍历所有文章
        for (Article article : articles) {
            String cid = article.getCid();
            //根据cid查询栏目表，查询栏目对象
            Category category = categoryMapper.selectByPrimaryKey(cid);
            article.setCid(category.getCname());
        }
        return articles;
    }

    @Override
    public List<Article> selectAllPreArticle() {
        List<Article> articles = articleMapper.selectAll();
        return articles;
    }

    @Override
    public List<Category> selectCategory() {
        List<Category> categoryList = categoryMapper.selectAll();
        return categoryList;
    }

}
